package com.hq.shop06back.controller;

import com.hq.commons.common.R;
import com.hq.commons.constatns.RabbitMQConstatns;
import com.hq.commons.ov.Goods;
import com.hq.shop06back.feign.api.GoodsService;
import com.hq.shop06back.vo.Stock;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("{model}/{page}.html")
    public String toPage(@PathVariable String model, @PathVariable String page) {
        return model + "/" + page;
    }

    /**
     * @author: HQ
     * @return:
     * @time: 2021/7/10 9:11
     * @description: 查询商品数据方法
     */
    @GetMapping("getGoods")
    public String getGoodsList(ModelMap modelMap) {
        List<Goods> goodsList = goodsService.getGoods();
        modelMap.put("goodsList", goodsList);
        return "goods/goodsLists";
    }


    /**
     * @author: HQ
     * @return:
     * @time: 2021/7/10 9:12
     * @description: 修改
     */
    @PostMapping("updateGoodsType")
    @ResponseBody
    public R updateGoodsType(Goods goods) {
        return R.write(goodsService.updateGoods(goods));
    }

    /**
     * @author: HQ
     * @return:
     * @time: 2021/7/10 9:12
     * @description: 删除
     */
    @GetMapping("deleteGoodsType/{id}")
    @ResponseBody
    public R deleteGoodsType(@PathVariable Integer id) {
        return R.write(goodsService.deleteGoods(id));
    }

    /**
     * @author: HQ
     * @return:
     * @time: 2021/7/10 14:37
     * @description: 添加
     */
    @PostMapping("/addGood")
    @ResponseBody
    public R addGoods(Goods goods) {
//        添加商品信息
        goods = goodsService.addGoodAndStock(goods);
//        发送数据到MQ
        rabbitTemplate.convertAndSend(RabbitMQConstatns.GOODS_EXCHANGE,"",goods);

        return R.success();
    }

}
